<?php
set_time_limit(120);
require_once("../../inc/global.php");
require_once("../MenuSys/function.php");
if(!isset($_GET['type']) || !isset($_GET['id'])){
	adminislogin();
}

/**
 创建目录
 */
function mkdirm($path){ 
	if (!file_exists($path)){ 
		mkdirm(dirname($path)); 
		mkdir($path, 0777); 
	} 
}

function listboxsubs($list,$id,$gang,$m){
	if(is_array($list)){
		if($gang==0){
			$gangstring="";
		}else{
			$gangstring='';
			for($j=1;$j<=$gang;$j++){
				if($j==$gang)
				{
					$gangstring=$gangstring."　|-";
				}
				else
				{
					$gangstring=$gangstring."　|";
				}
			}
		}
		$optionitem='';
		for($i=0;$i<count($list);$i++){
			if($list[$i][2]==$id){
				$optionitem.="<option value=\"".$m.'_'.$list[$i][0]."\">".$gangstring.$list[$i][1]."</option>\n";
				$optionitem.=listboxsubs($list,$list[$i][0],($gang+1),$m);
			}
		}
		return $optionitem;
	}
}
$data=$db->getlistarr('select * from menusys where menusys0>3 and char_length(menusys19)>1');
ob_start();
$html->includetop("","style=\"width:98%\";");
if(isset($_GET['type']) && isset($_GET['id'])){
	$globalurl = 'http://'.$_SERVER['HTTP_HOST'].'/'.ADMINROOT.'/System/htmlSys/createhtml.php';
	$type=$_GET['type'];
	$id=$_GET['id'];
	$id=explode('_',$id);
	$m=null;
	foreach($data as $val){
		if(intval($val['menusys0'])==intval($id[0])){
			$m=$val;
			break;
		}
	}
	
	$table=$m['menusys8'];
	$pathinfo=pathinfo(SITEROOT.$m['menusys19']);
	if($type=='type1'){ # 单页静态
	   	$t1=explode(',',$_GET['id']);
		$num1=count($t1);
		for($i=0;$i<$num1;$i++){
			$vid=$t1[$i];
			foreach($data as $val){
				if(intval($val['menusys0'])==intval($vid)){
					$m=$val;
					break;
				}
			}
			$pathinfo=pathinfo(SITEROOT.$m['menusys19']);
			/*
			if(!empty($m['menusys12']) && !empty($m['menusys13']) && !empty($m['menusys14']) && !empty($m['menusys15']) && !empty($m['menusys16']) && !empty($m['menusys17']) && !empty($m['menusys18'])){
				$filename=$m['menusys19'].'index.html';
			}else{
				if(isset($pathinfo['extension'])){
					if($m['menusys19']=='/' || empty($m['menusys19'])){
						$filename='/'.$vid.'.html';
					}else{
						$filename=($m['menusys19']{0}=='/')?$m['menusys19']:'/'.$m['menusys19'];
					}
				}else{
					$filename=$m['menusys19'].$vid.'.html';
				}
			}
			exit($filename);
			*/
				
			//echo '<pre>';
			//print_r($m);
			//exit();
			
			$url=$globalurl . '?type=onepage&menuid='.$vid;
			
			if((!empty($m['menusys13']) && !empty($m['menusys19'])) || (!empty($m['mmenusys13']) && !empty($m['mmenusys19']))){

				$status=get_headers($url,1);
				if(strpos($status[0],'200')){
					## PC端;
					if(!empty($m['menusys13']) && !empty($m['menusys19'])){
						if($status['PC-Status']=='ok'){
							echo '<a href="http://'.$_SERVER['HTTP_HOST'].$status['PC-Static'].'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$status['PC-Static'].'</a> <font color="#090">静态生成成功！</font>';
						}else{
							echo '<font color="red">静态生成失败！页面无法访问！</font>';
						}
						
					}
					
					## PC端;
					if(!empty($m['mmenusys13']) && !empty($m['mmenusys19'])){
						if($status['M-Status']=='ok'){
							echo '<a href="http://'.$_SERVER['HTTP_HOST'].$status['M-Static'].'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$status['M-Static'].'</a> <font color="#090">静态生成成功！</font>';
						}else{
							echo '<font color="red">静态生成失败！页面无法访问！</font>';
						}
					}
				}else{
					header('HTTP/1.1 500 Internal Server Error');
					echo '<font color="red">静态生成失败！页面无法访问！</font>';
				}
				
			}
		
		}
		
		
	}elseif($type=='type2'){ # 列表页静态
		if($m['menusys14']==''){
			$htmlpagepath = $m['menusys19'].'/list{$cid}_{$page}.html';
		}else{
			$htmlpagepath = $m['menusys14'];
			
		}
		$filename = $htmlpagepath;
		$htmlpagepath=base64_encode(gzcompress($htmlpagepath,9));
	 	$htmlpagepath=strtr($htmlpagepath,array('/'=>'_'));
		
		$url=$globalurl . '?type=list&menuid=' . $id[0] . '&classid=' . $id[1] .'&htmlpagepath='.$htmlpagepath;

		$pages=1;
		for($i=1;$i<=$pages;$i++){
			$surl=$url . '&page=' . $i;
			$status=get_headers($surl);
			if(strpos($status[0],'200')){
				$data=file_get_contents($surl);
				$fname=strtr($filename,array('{$page}'=>$i,'{$cid}'=>$id[1]));
				$p=pathinfo(SITEROOT.$fname);
				mkdirm($p['dirname']);
				file_put_contents(SITEROOT.$fname,$data);
				$pages++;
				flush();
				ob_flush();
				echo '<a href="http://'.$_SERVER['HTTP_HOST'].$fname.'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$fname.'</a> <font color="#090">静态生成成功！</font>';
				if(DEBUG) echo '<font style="color:#999999">'.$surl.'</font>';
				echo '<br />';
			}else{
				if($i==1){
					echo $surl;
					echo '<font color="red">生成失败！页面无法访问！</font><br />';
				}else{	
					echo '<font color="red">静态生成完成！</font><br />';
					break;	
				}
			}
		}
	}elseif($type=='type3'){   # 内容页静态
		
		$menuid=$_GET['id'];

		$htmlpagepath = $m['menusys16'];	
	
		$filename = $htmlpagepath;
		$htmlpagepath=base64_encode(gzcompress($htmlpagepath,9));
	 	$htmlpagepath=strtr($htmlpagepath,array('/'=>'_'));
		
		$url=$globalurl . '?type=detail&menuid=' . $menuid . '&htmlpagepath='.$htmlpagepath;
		
		mkdirm(SITEROOT.$m['menusys19']);
		if(isset($_GET['contentid'])){
			
			$contentid=$_GET['contentid'];
			$detail=$db->getoners('select '.$table.'0 from '.$table.' where '.$table.'0='.$contentid.' limit 1 ');
			$surl = $url . '&id=' . $contentid;
			$status=get_headers($surl);
			if(strpos($status[0],'200')){
				$data=file_get_contents($surl);
				$fname=strtr($filename,array('{$id}'=>$contentid));
				file_put_contents(SITEROOT.$fname,$data);
				$s=$db->query('update '.$table.' set '.$table.'2="'.$fname.'" where '.$table.'0='.$contentid. ' limit 1');
				echo  '<a href="http://'.$_SERVER['HTTP_HOST'].$fname.'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$fname.'</a> <font color="#090">静态生成成功！</font><br />';
			}else{
				header("HTTP/1.1 404 Not Found3");
				exit();
			}
			
		}else{
			/*
			$detail=$db->getlistarr('select '.$table.'0 from '.$table.' where '.$table.'1='.$menuid.' order by '.$table.'0 desc');
			foreach($detail as $val){
				$surl = $url . '&id=' . $val[$table.'0'];
				$status=get_headers($surl);
				if(strpos($status[0],'200')){
					$data=file_get_contents($surl);
					$fname=strtr($filename,array('{$id}'=>$val[0]));
					file_put_contents(SITEROOT.$fname,$data);
					$s=$db->query('update '.$table.' set '.$table.'2="'.$fname.'" where '.$table.'0='.$val[0]. ' limit 1');
					flush();
					ob_flush();
					echo  '<a href="http://'.$_SERVER['HTTP_HOST'].$fname.'" target="_blank">http://'.$_SERVER['HTTP_HOST'].$fname.'</a> <font color="#090">静态生成成功！</font>';
					if(DEBUG) echo '<font style="color:#999999">'.$surl.'</font>';
					echo '<br />';
				}else{
					echo $surl;
					echo '<font color="red">生成失败！页面无法访问！</font><br />';
				}
			}
			echo '<font color="red">静态生成完成！</font><br />';
			*/
		}
		
	}
}else{



?>
<script type="text/javascript">
function jump(id,val){
	document.getElementById('createhtml').src='status.php?status=1';
	if(val==null){
		var val=document.getElementById(id).value;
	}
	document.getElementById('createhtml').src='?type='+id+'&id='+val;
	//window.location.href='?type='+id+'&id='+val;
}
</script>
<fieldset>
    <legend style="font-size:14px;">静态页面生成</legend>
    单页面生成：
    <select id="type1" name="type1">
    <option value="0">请选择..</option>
    <?php
	$tval=array();
    foreach($data as $key=>$val){
        if(strtolower($val['menusys8'])=='onepagesys'){
            echo '<option value="'.$val[0].'">'.$val[1].'</option>'."\n";
			$tval[]=$val[0];
        }
    }
	$tvalstr=implode(',',$tval);
    ?>
    </select>
    <input type="button" value=" 生成 " onclick="jump('type1')" />
    <input type="button" value=" 生成所有单页面 " onclick="jump('type1','<?php echo $tvalstr;?>')" />
    
    
    <br />
<br />
    列表页生成：
    <select id="type2" name="type2">
    <option value="0">请选择...</option>
    <?php
    foreach($data as $val){
        if(strtolower($val['menusys8'])!='onepagesys' && $val['menusys15']!=''){
            if($val['menusys18']==0){
                echo '<option value="'.$val[0].'">'.$val[1].'</option>'."\n";
            }else{
                echo '<option value="'.$val[0].'" style="font-weight:bold" disabled>'.$val[1].'</option>'."\n";
                $d=$db->getlistarr("select * from classsys where classsys4=".$val[18]);
                echo listboxsubs($d,0,1,$val[0]);
            }
        }
    }
    ?>
    </select>
    <input type="button" value=" 生成 " onclick="jump('type2')" />
    
    <br />
<br />
    内容页生成：
    <select id="type3" name="type3">
    <option value="0">请选择...</option>
    <?php
    foreach($data as $val){
        if(strtolower($val['menusys8'])!='onepagesys' && $val['menusys16']!=''){
                echo '<option value="'.$val[0].'">'.$val[1].'</option>'."\n";
        }
    }
    ?>
    </select>
    <input type="button" value=" 生成 " onclick="jump('type3')" />
    <br />
</fieldset>
<fieldset>
<legend style="font-size:14px;">静态页面生成</legend>
<iframe width="100%" height="220px" frameborder="0" id="createhtml" src="status.php?status=0" scrolling="auto"></iframe>
</fieldset>
<script type="text/javascript">try{document.getElementById('t').value='<?php echo $_GET['t'];?>';}catch(e){};</script>
<?php
}
$html->includeend();
?>
